package pile.customer.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pile.customer.config.CustomerServiceWebSocketServerUtil;
import pile.customer.config.UserWebSocketServerUtil;
import pile.customer.entity.Information;
import pile.customer.entity.User;
import pile.customer.params.ReceivingInformation;
import pile.customer.service.IInformationService;
import pile.customer.service.IUserInformationService;
import pile.customer.service.IUserService;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserInformationServiceImpl implements IUserInformationService {

    @Autowired
    private CustomerServiceWebSocketServerUtil customerServiceWebSocketServerUtil;


    @Autowired
    private IInformationService informationService;

    @Autowired
    private IUserService userService;

    @Autowired
    private UserWebSocketServerUtil userWebSocketServerUtil;

    /**
     * 处理信息
     *
     * @param receivingInformation
     */
    @Override
    public void processingMessages(ReceivingInformation receivingInformation) throws Exception {


        //插入数据库数据
        Information information = new Information();

        information.setMessage(receivingInformation.getMessage());
        information.setOpenId(receivingInformation.getOpenId());
        //类型
        information.setType(1);
        informationService.add(information);

        //根据openId 查询用户表
        User user = userService.selectByOpenId(receivingInformation.getOpenId());

        Boolean automaticReply = receivingInformation.getAutomaticReply();
        if(automaticReply){
            //自动回复
            receivingInformation.setMessage(receivingInformation.getReply());
            try {
            userWebSocketServerUtil.sendInfo(JSONUtil.toJsonStr(receivingInformation), receivingInformation.getOpenId());
            } catch (Exception e) {
                log.error("用户发送给客服消息报错:" + e);
            }

            //插入数据库数据
            Information informationReply = new Information();

            informationReply.setMessage(receivingInformation.getMessage());
            informationReply.setOpenId(receivingInformation.getOpenId());
            //类型
            information.setType(2);
            informationService.add(information);
        }
        if(user.getIsDisplay()==1 && !automaticReply){
            receivingInformation.setCustomerServiceAccount(user.getCustomerServiceAccount());
        }
        //判断用户是否接入 还有是否 显示  这代表客服已经跟用户连接了
        //if(user.getIsDisplay()==1 && !receivingInformation.getCustomerServiceAccount().equals("null")){
        //是要显示在列表 代表已经接入了 kel  这里要给客服直接发送消息
        try {
            //找到在线客服 给客服发送消息
            String message = JSONUtil.toJsonStr(receivingInformation);
            String targetAccount = receivingInformation.getCustomerServiceAccount();

            // 使用三元表达式简化条件判断和消息发送逻辑
            customerServiceWebSocketServerUtil.sendInfo(message, targetAccount == null ? null : targetAccount);
            log.info("发送给客服消息成功:" + JSONUtil.toJsonStr(receivingInformation));
        } catch (Exception e) {
            log.error("用户发送给客服消息报错:" + e);
        }


        if (user.getIsDisplay() == 2) {
            //不显示 要添加未接入数量
            user.setNoAccessNum(user.getNoAccessNum() + 1);
            //未读消息数量
            user.setUnreadQuantity(user.getUnreadQuantity() + 1);
        }
        //修改用户表里面 最后一次发送时间

        //跟新最后一次发送信息
        user.setLastMessage(receivingInformation.getMessage());
        //最后一次发送的时间
        user.setLastMessageTime(new Date());
        //最后一次发送消息的类型
        user.setLastMessageType(1);

        userService.updateById(user);

    }
}
